﻿using Project.API.Models;
using Project.BLL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.UI;
using System.Xml.Linq;

namespace Project.API.Controllers
{
    [ApiAuthorize]
    public class UsersController : ApiController
    {
        // GET api/<controller>
        [HttpGet]
        public ResultPageList<UsersDto> Get(int page, int limit, string name)
        {

            int count = 0;
            var list = new UsersBll().GetUsersByPage(page, limit, name, out count);
            var roles = new RolesBll().Query();
            var data = from u in list
                       select new UsersDto
                       {
                           userID = u.userID,
                           loginName = u.loginName,
                           loginPwd = u.loginPwd,
                           userName = u.userName,
                           sex = u.sex,
                           birthday = u.birthday,
                           phone = u.phone,
                           imgID = u.imgID,
                           roleID = u.roleID,
                           status = u.status,
                           lastTime = u.lastTime,
                           roleName = u.Roles.roleName,
                           deptID = u.Roles.Department.deptID,
                           deptName = u.Roles.Department.deptName,
                           title = u.Images.title,
                           path = u.Images.path,
                           extension = u.Images.extension,
                       };
            ResultPageList<UsersDto> result = new ResultPageList<UsersDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();
            return result;
        }

        //获取员工信息
        // GET api/<controller>/5
        public ResultData<List< UsersDto>> Get(int id)
        {

            int count = 0;
            var list = new UsersBll().GetUsers(id);
            var roles = new RolesBll().Query();
            var data = from u in list
                       select new UsersDto
                       {
                           userID = u.userID,
                           loginName = u.loginName,
                           loginPwd = u.loginPwd,
                           userName = u.userName,
                           sex = u.sex,
                           birthday = u.birthday,
                           phone = u.phone,
                           imgID = u.imgID,
                           roleID = u.roleID,
                           status = u.status,
                           lastTime = u.lastTime,
                           roleName = u.Roles.roleName,
                           deptID = u.Roles.Department.deptID,
                           deptName = u.Roles.Department.deptName,
                           title = u.Images.title,
                           path = u.Images.path,
                           extension = u.Images.extension,
                       };
            ResultData<List<UsersDto>> result = new ResultData<List<UsersDto>>();
            result.code = 0;
            result.msg = "ok";
            result.data = data.ToList();
            return result;
        }

        // POST api/<controller>
        public Result Post([FromBody] Users value)
        {
            Users users = new Users();
            users.loginName = value.loginName;
            users.loginPwd = MD5Helper.Md5(value.loginPwd, "X2");
            users.userName = value.userName;
            users.sex = value.sex;
            users.birthday = value.birthday;
            users.phone = value.phone;
            users.imgID = 1;
            users.roleID = value.roleID;
            users.status = true;
            users.lastTime = DateTime.Now;
            bool bol = new UsersBll().Add(users);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息添加成功!" : "error";
            return result;
        }

        // PUT api/<controller>/5
        public Result Put(int id, [FromBody] Users value)
        {
         
            value.lastTime = DateTime.Now;
            bool bol = new UsersBll().Update(value);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "修改成功!" : "error";
            return result;
        }

        // DELETE api/<controller>/5
        public Result Delete(int id)
        {
            var list = new UsersBll().Query().Where(x => x.userID == id).FirstOrDefault();
            bool bol = new UsersBll().Delete(list);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "删除成功!!" : "error";
            return result;
        }
    }
}